package com.mic.controller;

import com.mic.pojo.Admin;
import com.mic.pojo.Article;
import com.mic.pojo.User;
import com.mic.service.AdminServiceImpl;
import com.mic.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    @Qualifier("AdminServiceImpl")
    AdminServiceImpl adminService;
    @Autowired
    @Qualifier("UserServiceImpl")
    UserServiceImpl userService;

    @RequestMapping("toLogin")
    public String toLogin(){
        return "login";
    }

    @RequestMapping("/login")
    public void login(Model model, @RequestParam("username") String username, @RequestParam("password") String password,
                      HttpServletResponse response, HttpServletRequest request, HttpSession session
                      ) throws IOException {
        List<User> users = adminService.queryAllUser();
        List<Admin> admins = adminService.queryAllAdmin();


        for(Admin admin:admins){
            if (admin.getUsername().equals(username)&&admin.getPassword().equals(password)){
                String message="管理员登录";
                session.setAttribute("users",users);
                model.addAttribute("message",message);
                response.sendRedirect(request.getContextPath()+"/toMainAdmin");
            }
        }

        for(User user:users){
            if(user.getUsername().equals(username)&&user.getPassword().equals(password)){
                String message="登录成功";
                session.setAttribute("userId",user.getId());//使用session对userId进行传输
                model.addAttribute("message",message);
                response.sendRedirect(request.getContextPath()+"/toMainUser");
            }
        }
        String message="用户名或密码错误";
        model.addAttribute("message",message);
    }


    @RequestMapping("/toMainAdmin")
    public String toMainAdmin(Model model,HttpServletRequest request, HttpServletResponse response,
                              HttpSession session){
        List<Article> articles = adminService.queryAllArticle();
        model.addAttribute("articles",articles);
        model.addAttribute("users",session.getAttribute("users"));
        model.addAttribute("user",session.getAttribute("user"));
        return "MainAdmin";
    }
    @RequestMapping("/toMainUser")
    public String toMainUser(HttpSession session, Model model){

        return "redirect:toBlogSquare";
    }

    @RequestMapping("/addUser")
    public void AddUser(@RequestParam("username") String username, @RequestParam("password") String password, HttpServletResponse response, HttpServletRequest request) throws IOException {
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        adminService.addUser(user);

        response.sendRedirect(request.getContextPath()+"/toLogin");
    }

}
